import cloud from '@lafjs/cloud'
const db = cloud.database();
let dbName = "admin-menus"


export default async function (ctx: FunctionContext) {
  let dataJson = { ...ctx.body.data }
  switch (ctx.body.type) {
    case "list"://列表
      let { data } = await db.collection(dbName).get();
      return cloud.utils.requestReturn({ data })
      break;
    case "query"://不分页
      let resp = await db.collection(dbName).get();
      return cloud.utils.requestReturn({ data: resp.data })
      break;
    case "add"://新增
      await cloud.nw.db.add({
        dbName, dataJson, cancelAddTime: false
      });
      return cloud.utils.requestReturn({ data: "" })
      break;
    case "update"://编辑
      delete dataJson._id
      await cloud.nw.db.update({
        dbName, whereJson: { _id: ctx.body.data._id }, dataJson
      });
      return cloud.utils.requestReturn({ data: "" })
      break;
    case "delete"://删除
      delete dataJson._id
      await cloud.nw.db.del({
        dbName, whereJson: { _id: ctx.body.data._id }
      });
      return cloud.utils.requestReturn({ data: "" })
      break;
    default:
      break;
  }
}
